﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class SubPolicyQuestionRepeat : System.Web.UI.Page
    {
        private int subId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request["subId"] != null && this.Request["subId"].ToString() != "")
            {
                subId = int.Parse(this.Request["subId"]);
            }
            if (!Page.IsPostBack)
            {
                if (subId > 0)
                {

                    TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                    if (sub != null && sub.Id != 0)
                    {
                        XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                        if (xmlQuestion.StartDate != null)
                        {
                            this.tbStartDate.Text = xmlQuestion.StartDate.Value.ToString("yyyy-MM-dd HH:mm");
                        }
                        if (xmlQuestion.EndDate != null)
                        {
                            this.tbEndDate.Text = xmlQuestion.EndDate.Value.ToString("yyyy-MM-dd HH:mm");
                        }
                        if (xmlQuestion.AppearNum != null && xmlQuestion.AppearNum!="" && xmlQuestion.AppearNum!="0")
                        {
                            this.drpNum.Value = xmlQuestion.AppearNum;
                        }
                    }
                }
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (subId>0)
            {

                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id !=0)
                {
                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    if (this.tbStartDate.Text != "" && this.tbEndDate.Text != "")
                    {
                        xmlQuestion.StartDate = DateTime.Parse(this.tbStartDate.Text);
                        xmlQuestion.EndDate = DateTime.Parse(this.tbEndDate.Text);
                        xmlQuestion.AppearNum = this.drpNum.Items[this.drpNum.SelectedIndex].Value;
                        sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                        TestPaperPolicySub.Edit(sub);
                        if (sub != null && sub.Id != 0)
                        {
                            string value = String.Format("从{0}至{1}已抽取{2}次的题目不选用",
                                                        this.tbStartDate.Text,
                                                        this.tbEndDate.Text, this.drpNum.Items[this.drpNum.SelectedIndex].Value);
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", " SetRepeater('" + value + "');", true);
                        }
                        else
                        {
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('失败');", true);
                        }
                    }
                }
            }
        }

        protected void tbCancel_Click(object sender, EventArgs e)
        {
            if (subId>0)
            {
                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id != 0)
                {
                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    xmlQuestion.StartDate = null;
                    xmlQuestion.EndDate = null;
                    xmlQuestion.AppearNum = null;
                    sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                    TestPaperPolicySub.Edit(sub);
                    if (sub != null && sub.Id != 0)
                    {
                        string value = "";
                        this.ClientScript.RegisterStartupScript(this.GetType(), "key", "SetRepeater('');", true);
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('失败');", true);
                    }

                }
            }
        }
    }
}
